package ms.core.gateway.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;

import ms.core.common.Page;
import ms.core.db.mysql.QueryParam;
import ms.core.gateway.bean.SysRoleBean;
import ms.core.gateway.request.*;
import ms.core.gateway.response.SysRoleVO;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;

public class SysRoleDao extends BaseDao{
	
	public SysRoleDao() { super("sys_role"); }
	
	/**
	 * 添加新角色
	 * 
	 * @param role 角色对象
	 * @param funcs 授权功能
	 * @return
	 */
	public void newRole(CreateRoleRequest role, List<String> funcs) {
		Map<String, Object> values = JsonTool.objectToMap(role, true);		
		if (!CollectionUtils.isEmpty(funcs))
			values.put("funcs", JsonTool.listBeanToJson(funcs));

		values.put("createTime", new Date());

		db.insertRecord(table, values);
	}
	
	/**
	 * 删除角色
	 * 
	 * @param id
	 * @return 0-角色不存在;1-成功
	 */
	public int deleteRole(long id) {
		return db.deleteRecord(table, id);
	}
	
	/**
	 * 修改角色
	 * 
	 * @param role
	 * @return 0-角色已存在;1-成功
	 */
	public int editRole(UpdateRoleRequest role) {
		String sql = String.format("update %s set roleName=?,root=?,remark=?,updateTime=? where id=?",table);
		return db.executeSql(sql, new Object[] {role.getRoleName(), role.getRoot(), role.getRemark(), new Date(), role.getId()});
	}

	/**
	 * 角色授权
	 * 
	 * @param id 角色记录id
	 * @param funcs 功能列表
	 * @return
	 */
	public int authFuncs(long id, List<String> funcs) {
		String sql = String.format("update %s set funcs=?,updateTime=? where id=?",table);
		return db.executeSql(sql, new Object[] {JsonTool.listBeanToJson(funcs), new Date(), id});
	}	

	/**
	 * 按账号获取角色信息
	 * 
	 * @param corpUid 企业Id
	 * @param roleName 角色名称
	 * @return
	 */
	public <T>T getRole(String corpUid, String roleName, Class<T> cls) {
		String sql = String.format("select * from %s where corpUid=? and roleName=?", table);
		return db.queryForBean(sql, cls, new Object[] {corpUid, roleName});
	}

	/**
	 * 按用记录Id获取角色信息
	 * 
	 * @param id
	 * @return
	 */
	public SysRoleBean getRole(long id) {
		String sql = String.format("select * from %s where id=?", table);
		return db.queryForBean(sql, SysRoleBean.class, new Object[] {id});
	}

	/**
	 * 按角色列表查询角色
	 * 
	 * @param corpUid 企业Id
	 * @param roles 角色列表
	 * @param name 角色名
	 * @return
	 */
	public List<SysRoleVO> queryRoles(String corpUid, List<String> roles, String name) {
		List<Object> params = new ArrayList<>(); 
		String sql = String.format("select * from %s where corpUid=?", table);
		params.add(corpUid);
		// 按角色名列表精准匹配
		if (roles!=null && roles.size()>0) {
			sql += " and (";
			for(int i=0; i<roles.size(); i++) {
				sql += " roleName=?";
				if (i<roles.size()-1) sql += " or ";
				params.add(roles.get(i));
			}
			sql += ")";
		}
		// 按角色名模糊匹配
		if (!StrTool.isBlank(name)) {
			sql += " and roleName like ?";
			params.add("%"+name+"%");
		}

		return db.queryForBeans(sql, SysRoleVO.class, params);
	}
	
	private String getCondition(QueryRoleRequest req, QueryParam qp){
		String ret = "";

		if (!StrTool.isBlank(req.getCorpUid())) {
			ret += " and corpUid=?";
			qp.addParam(req.getCorpUid());
		}
		if (!StrTool.isBlank(req.getRoleName())) {
			ret += " and roleName=?";
			qp.addParam(req.getRoleName());
		}

		return ret;
	}
	
	public Page<SysRoleVO> queryRole(QueryRoleRequest req) {
		QueryParam qp = QueryParam.getInstance().setPage(req.getPage(), req.getPagesize());
		String sqlWhere =getCondition(req, qp);
		String countSql = String.format("select count(*) from %s where 1>0", table) + sqlWhere;
		String querySql = String.format("select * from %s where 1>0", table) + sqlWhere + " order by id";

		qp.setCntSql(countSql).setQrySql(querySql);
		return pageQuery(qp, SysRoleVO.class);		
	}
}
